-- MonsterCaptainM
-- Created by panyl
-- 海怪船长

module("MonsterCaptainM", package.seeall);

local captainCfg = {};

local whenRefreshProp;

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    captainCfg = {};

    -- 转换下monster_captain信息表
    local monster_captain_CSV = get_monster_captain_CSV();
    for i = 1, #monster_captain_CSV do
        captainCfg[monster_captain_CSV[i].class_id] = monster_captain_CSV[i];
    end
end

function init()
    loadCsv();

    EventMgr.removeAll("MonsterCaptainM");
end

function query(classId, path)
    local m = captainCfg[classId];

    if not m or not path then
        return m;
    end

    return m[path];
end

-- 获取所有的可穿戴装备
function getAllEquipments()
    local ret = {};

    for classId, info in pairs(captainCfg) do
        if info["equipment"] == 1 then
            ret[info["pos"]] = classId;
        end
    end

    return ret;
end

-- 获取所有的可喂食物品
function getAllFeedItems()
    local ret = {};

    for classId, info in pairs(captainCfg) do
        if info["equipment"] == 0 then
            table.insert(ret, classId);
        end
    end

    return ret;
end

-- 是否海怪船长
function isMonsterCaptain(summon)
    if not summon then
        return;
    end

    -- 如果不是召唤兽
    if summon.type ~= OBJECT_TYPE_SUMMON then
        return false;
    end

    return SummonM.query(summon.classId, "rule") == "monster_captain";
end

-- 获取食物属性
function getFoodProps(pos)
    local grid = DungeonM.getGridByPos(pos);
    local summon = grid.summon;

    if not isMonsterCaptain(summon) then
        return {};
    end

    -- 先把所有的属性加成累计起来
    local summonInfo = ME.user.dbase:query("summon", {});
    if not summonInfo[pos] or not summonInfo[pos]["food"] then
        return {};
    end
    local food   = summonInfo[pos]["food"];
    local props  = {};
    local propId = PropM.getPropId("attrib2");
    for classId, amount in pairs(food) do
        local apply = query(classId, "apply") or {};
        local attrib = apply["attrib"] or {};

        for key, value in pairs(attrib) do
            table.insert(props, { propId, key, value * amount, -1, });
        end
    end

    -- food下的属性
    props = table.append(props, table.deepcopy(summon.dbase:queryTemp("prop", "food", {})));

    -- 合并
    local ret = {};
    local len = #props;
    for i = 1, len do
        if props[i] then
            for j = i + 1, len do
                if props[j] and props[i][1] == props[j][1] and props[i][2] == props[j][2] then
                    -- i、j是同一条属性，合并到i上
                    props[i][3] = props[i][3] + props[j][3];
                    props[j] = nil;
                end
            end

            table.insert(ret, props[i]);
        end
    end

    return ret;
end

-- 获取装备属性
function getEquipmentProps(summon)
    local equipments = summon.equipments or {};
    local ret = {};

    for _, classId in pairs(equipments) do
        local apply = query(classId, "apply") or {};
        for key, value in pairs(apply) do
            if "prop" == key then
                local props = table.deepcopy(value);

                if type(props[1]) == "number" then
                    props = { props, };
                end

                ret = table.append(ret, props);
            end
        end
    end

    return ret;
end

-- 获取喂食次数
function getFeedTimes(pos, classId)
    local summonInfo = ME.user.dbase:query("summon", {});
    if not summonInfo[pos] or not summonInfo[pos]["food"] then
        return 0;
    end

    return summonInfo[pos]["food"][classId] or 0;
end

-- 喂食
function feed(pos, classId)
    local grid = DungeonM.getGridByPos(pos);
    local summon = grid.summon;

    -- 如果不是海怪船长
    if not summon or
        SummonM.query(summon.classId, "rule") ~= "monster_captain" then
        return false;
    end

    if query(classId, "equipment") == 1 then
        -- 装备走在装备流程
        return false;
    end

    -- 道具不足
    if ItemM.getAmount(ME.user, classId) <= 0 then
        trace("MonsterCaptainM", "%d不足。", classId);
        return false;
    end

    -- 先扣除
    ItemM.costAmount(ME.user, classId, 1);

    -- 效果
    doApply(summon, classId, "food");

    local summonInfo = ME.user.dbase:query("summon", {});
    if summonInfo[pos] then
        summonInfo[pos]["food"] = summonInfo[pos]["food"] or {};

        summonInfo[pos]["food"][classId] = (summonInfo[pos]["food"][classId] or 0) + 1;
    end

    -- 抛出提示
    local msg = query(classId, "tip");
    if msg and #msg > 0 then
        doAlert(msg);
    end

    -- 抛出事件
    EventMgr.fire(event.FEED_MONSTER_CAPTAIN, {["pos"] = pos, ["classId"] = classId, ["summon_id"] = summon.classId, });
    return true;
end

-- 装备
function doEquip(pos, classId)
    local grid = DungeonM.getGridByPos(pos);
    local summon = grid.summon;

    -- 如果不是海怪船长
    if not summon or
        SummonM.query(summon.classId, "rule") ~= "monster_captain" then
        return false;
    end

    local data = query(classId);
    if not data or data["equipment"] ~= 1 then
        return false;
    end

    -- 道具不足
    if ItemM.getAmount(ME.user, classId) <= 0 then
        trace("MonsterCaptainM", "%d不足。", classId);
        return false;
    end

    local equipments = summon.equipments or {};
    -- 如果已经穿戴了
    if equipments[data["pos"]] then
        return false;
    end

    -- 先扣除
    ItemM.costAmount(ME.user, classId, 1);

    equipments[data["pos"]] = classId;
    summon.equipments = equipments;

    local summonInfo = ME.user.dbase:query("summon", {});
    if summonInfo[pos] then
        summonInfo[pos]["equipments"] = equipments;
    end

    -- 效果
    doApply(summon, classId, "equip");

    -- 刷新下属性
    PropM.refreshEffect(summon, "equip");

    -- 抛出事件，ui关注刷新怪物攻击
    EventMgr.fire(event.REFRESH_MONSTER_INFO);

    return true;
end

-- 效果
function doApply(summon, classId, path)
    local apply = query(classId, "apply") or {};

    for key, value in pairs(apply) do
        if key == "attrib" then
            for attrib, v in pairs(value) do
                summon:addAttrib(attrib, v);
            end
        end
    end
end
